package com.five.community.controller;

import com.five.community.bean.*;
import com.five.community.bean.vo.ChargedetailsmanagementVo;
import com.five.community.bean.vo.ChargeprojectVo;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.PersonVo;
import com.five.community.contant.AppContants;
import com.five.community.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chargedetailsmanagement")
public class ChargedetailsmanagementController {
    @Autowired
    private ChargedetailsmanagementService chargedetailsmanagementService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private ChargeprojectService chargeprojectService;
    @Autowired
    private  ChargedetailsmanagerService chargedetailsmanagerService;
    @Autowired
    private PersonService personService;

    //显示收费项目一览画面
    @RequestMapping("/listUi")
    public String listUi( ModelMap map,Integer pageNum,String pservicecode) {
  if (pageNum == null) {
            pageNum = 1;
        }
      if(pservicecode == null || pservicecode == "") {
             try {
                 PageInfo<ChargedetailsmanagementVo> pageInfo = chargedetailsmanagementService.getPageInfo(pageNum, AppContants.chargepageSize);
                 map.addAttribute("pageInfo", pageInfo);
             } catch (Exception e) {
                 e.printStackTrace();
             }
      }else {
          try {
              PageHelper.startPage(pageNum, AppContants.chargepageSize);
              List<ChargedetailsmanagementVo> chargedetailsmanagementVos = chargedetailsmanagementService.selectByPservicecode(pservicecode);
              PageInfo<ChargedetailsmanagementVo> pageInfo = new PageInfo<>(chargedetailsmanagementVos);
              map.addAttribute("pageInfo", pageInfo);
              ChargedetailsmanagementVo chargedetailsmanagementVo = new ChargedetailsmanagementVo();
              chargedetailsmanagementVo.setPservicecode(pservicecode);
              map.addAttribute("chargedetailsmanagementVo", chargedetailsmanagementVo);
          }catch (Exception e) {
              e.printStackTrace();
          }
      }






//        try {
            //假设第一个启动页面，当前页数为1，每页显示数量4
            //通过业务层获得分页信息
            // PageInfo<Map<String,Object>> pageInfo=chargedetailsmanagementService.getPageInfo(1,4);

//            List<ChargedetailsmanagementVo> chargedetailsmanagementVos = chargedetailsmanagementService.selectAll();
//            map.addAttribute("chargedetailsmanagementVos", chargedetailsmanagementVos);
//            chargedetailsmanagementVos.forEach(System.out::println);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //System.out.println("11111111");
        return "chargedetailsmanagement_list";//返回出一个所有收费项目的画面
    }
    //根据输入的服务号进行模糊查询
    @RequestMapping("/select")
    public String select(String pservicecode,ModelMap map){
        if(pservicecode == ""){
            return "redirect:/chargedetailsmanagement/listUi";
        }
        else{
            try {
                List<ChargedetailsmanagementVo> chargedetailsmanagementVos = chargedetailsmanagementService.selectByPservicecode(pservicecode);
                map.addAttribute("chargedetailsmanagementVos", chargedetailsmanagementVos);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "chargedetailsmanagement_list";
        }
    }

    @RequestMapping("/addUi")
    public String addUi(ModelMap map){
        try {
           List<Village> villages = villageService.selectAll();
           map.addAttribute("villages", villages);
           List<Chargedetailsmanager> chargedetailsmanagers = chargedetailsmanagerService.selectAll();
           map.addAttribute("chargedetailsmanagers", chargedetailsmanagers);
           List<Person> persons =personService.selectAll();
            map.addAttribute("persons",persons);
           List<Chargeproject> chargeprojects =chargeprojectService.selectAllChargeproject();
           map.addAttribute("chargeprojects",chargeprojects);
           } catch (Exception e) {
             e.printStackTrace();
          }
        return "chargedetailsmanagement_add";
    }
    @RequestMapping("/add")
     public String add(ChargedetailsmanagementVo chargedetailsmanagementVo){
        int id1 = Integer.parseInt(chargedetailsmanagementVo.getVname());
        int id2 =Integer.parseInt(chargedetailsmanagementVo.getPname());
        int id3 =Integer.parseInt(chargedetailsmanagementVo.getTname());
        int id4 =Integer.parseInt(chargedetailsmanagementVo.getHname());
        try{
              Chargedetailsmanagement chargedetailsmanagement =new Chargedetailsmanagement(
                    chargedetailsmanagementVo.getId(),
                    chargedetailsmanagementVo.getMark(),
                    chargedetailsmanagementVo.getPracticalPayment(),
                    chargedetailsmanagementVo.getAlreadyPayment(),
                      id1,
                      id2,
                      id3,
                      id4);
              chargedetailsmanagementService.insert(chargedetailsmanagement);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
        return "chargedetailsmanagement_add";
    }

    @RequestMapping("/editUi")
    public String editUi(int id,ModelMap map){
        try {

            Chargedetailsmanagement chargedetailsmanagement =chargedetailsmanagementService.selectbyid(id);
            map.addAttribute("chargedetailsmanagement",chargedetailsmanagement);
            List<Chargedetailsmanager> chargedetailsmanagers=chargedetailsmanagerService.selectAll();
            map.addAttribute("chargedetailsmanagers",chargedetailsmanagers);
            List<Village> villages=villageService.selectAll();
            map.addAttribute("villages",villages);
            List<Chargeproject> chargeprojects=chargeprojectService.selectAllChargeproject();
            map.addAttribute("chargeprojects",chargeprojects);
            List<Person> persons=personService.selectAll();
            map.addAttribute("persons",persons);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "chargedetailsmanagement_edit";
    }

    @RequestMapping("/edit")
    public String edit(ChargedetailsmanagementVo chargedetailsmanagementVo){
        int id5 =Integer.parseInt(chargedetailsmanagementVo.getVname());
        int id6 =Integer.parseInt(chargedetailsmanagementVo.getPname());
        int id7 =Integer.parseInt(chargedetailsmanagementVo.getTname());
        int id8 =Integer.parseInt(chargedetailsmanagementVo.getHname());
        System.out.println("chargeprojectVo.getId() = " + chargedetailsmanagementVo.getId());
        try{
            Chargedetailsmanagement chargedetailsmanagement=new Chargedetailsmanagement(
                    chargedetailsmanagementVo.getId(),
                    chargedetailsmanagementVo.getMark(),
                    chargedetailsmanagementVo.getPracticalPayment(),
                    chargedetailsmanagementVo.getAlreadyPayment(),
                    id5,
                    id6,
                    id7,
                    id8
            );
                  chargedetailsmanagementService.update(chargedetailsmanagement);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  "chargedetailsmanagement_edit";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        try{
            chargedetailsmanagementService.deleteById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return "redirect:/chargedetailsmanagement/listUi";
    }
}
